import Vue from 'vue'
import App from './App.vue'
import router from './router'

import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
import qs from 'qs'
import {
	getCurDate,
	setSessionStorage,
	getSessionStorage,
	removeSessionStorage,
	setLocalStorage,
	getLocalStorage,
	removeLocalStorage,
} from './common.js'
//设置axios的基础url部分
//axios.defaults.baseURL = 'http://localhost:8080/elm/';
axios.defaults.baseURL = 'http://localhost:14000/';

//axios.defaults.baseURL = 'http://172.23.172.118:8080/elm/';
//设置响应拦截器
axios.interceptors.response.use(function(response) {
	if (response.data.code == 403) {
		location.href = '/error403';
	}
	return response;
}, function(error) {
	console.log(error);
	return Promise.reject(error);
});

//将axios挂载到vue实例上，使用时就可以 this.$axios
Vue.prototype.$axios = axios;
Vue.prototype.$getCurDate = getCurDate;
Vue.prototype.$qs = qs;
Vue.prototype.$setSessionStorage = setSessionStorage;
Vue.prototype.$getSessionStorage = getSessionStorage;
Vue.prototype.$removeSessionStorage = removeSessionStorage;
Vue.prototype.$setLocalStorage = setLocalStorage;
Vue.prototype.$getLocalStorage = getLocalStorage;
Vue.prototype.$removeLocalStorage = removeLocalStorage;

router.beforeEach((to, from, next) => {
	let user = sessionStorage.getItem('user');
	//除了登陆、注册、首页、商家列表、商家信息之外，都需要判断是否登录
	if (!(to.path == '/' || to.path == '/index' || to.path == '/businessList' || to.path == '/businessInfo' ||
			to.path == '/login' || to.path == '/register'||to.path == '/error403')) {
		if (user == null) {
			router.push('/login');
			location.reload();
		}
	}
	next();
});

router.afterEach((to, from, next) => {
	// 路由跳转之后，页面重新定位到顶部
	window.scrollTo(0, 0);
})

Vue.config.productionTip = false

new Vue({
	router,
	render: h => h(App)
}).$mount('#app')
